<?php
/*
   Copyright 2012 BiSe Trojanov

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
 require_once('config.php');

?><!doctype html><html>
<head>
 <?php include('./include/header.inc'); ?>
 <title>Edit Code / <?php echo $sad_domain; ?></title>
 
 <link rel="stylesheet" href="<?php echo $sad_baseurl; ?>/i/style/edit.css" type="text/css" media="screen">
</head>
<body>
<?php include('./include/toolbar.inc');?>

<?php
 if (!iamd()){header('http/1.0 403 For developers');echo 'Sorry. For developer only.</body></html>';exit;}

 $f1  =sad_safe_path($_POST['file']);
 if (strlen($f1)>0){
  $file=$sad_root.'/templates/'.$f1;

  $fw=fopen($file,'w');
  if ($fw==false){
   echo '<span style="color: red">Can not save file</span><br/>';
  }else{
   fwrite($fw,$_POST['buf']);
   fclose($fw);
   echo '<span style="color: green">File saved</span><br/>';
  }
 }else{
  $f1  =sad_safe_path($_GET['folder'].'/'.$_GET['file']);
  $file=$sad_root.'/templates/'.$f1;
 }
 echo $file;
 error_reporting(~E_ALL);
 
 $special=false;
 echo '<script>document.title="'.sad_safe_mysql(preg_replace('_^.+?/([^/]+)$_','$1',$f1)).'"+" / "+document.title;</script>';
?><form method="post" action="?file=<?php echo urlencode($f1);?>">

<?php if (preg_match('|^buttons/|',$f1)){$special=true; ?>

 <span style="float: right">
  <a href="javascript:" onclick="but_add();"><img src="<?php echo $sad_baseurl;?>/i/style/plus.png" style="border: none;"></a><br />
  <a href="javascript:" onclick="but_sort();"><img src="<?php echo $sad_baseurl;?>/i/style/arrow-switch.png" style="border: none;"></a><br />
  <a href="javascript:" onclick="but_delete();"><img src="<?php echo $sad_baseurl;?>/i/style/cross.png" style="border: none;"></a><br />
  <a href="javascript:" onclick="but_edit();"><img src="<?php echo $sad_baseurl;?>/i/style/receipt--pencil.png" style="border: none;"></a><br />
  <a href="javascript:" onclick="but_editcode();"><img src="<?php echo $sad_baseurl;?>/i/style/blue-document-attribute-p.png" style="border: none;"></a><br />
  <a href="javascript:" onclick="but_options();"><img src="<?php echo $sad_baseurl;?>/i/style/wand.png" style="border: none;"></a><br />
 </span>

<script>
 function but_add(){
  document.getElementById('buf').value+="\r\n"+'<?php
   echo '<a href="/i/add.php?table={#table#}&amp;edit=textandtitle&amp;parent=parent" target="_blank" title="Add"><img src="/i/style/plus.png" style="border: none;" alt=" add "></a>';
  ?>';
 }

 function but_sort(){
  document.getElementById('buf').value+="\r\n"+'<?php
   echo ('<a href="/i/sort.php?table={#table#}&amp;title=title&amp;parent=parent&amp;edit=textandtitle" target="_blank" title="Sort"><img src="/i/style/arrow-switch.png" style="border: none;" alt=" sort "></a>');
  ?>';
 }

 function but_delete(){
  document.getElementById('buf').value+="\r\n"+'<?php
   echo ('<a href="/i/delete.php?table={#table#}&amp;id={#id#}" target="_blank" title="DELETE"><img src="/i/style/cross.png" style="border: none;" alt=" delete "></a>');
  ?>';
 }

 function but_edit(){
  document.getElementById('buf').value+="\r\n"+'<?php
   echo ('<a href="/i/edit.php?table={#table#}&amp;id={#id#}&amp;edit=textandtitle" target="_blank" title="Edit"><img src="/i/style/receipt--pencil.png" style="border: none;" alt=" edit "></a>');
  ?>';
 }

 function but_options(){
  document.getElementById('buf').value+="\r\n"+'<?php
   echo ('<a href="/i/options.php" target="_blank" title="Edit main options"><img src="/i/style/wand.png" style="border: none;" alt=" Edit main options "></a>');
  ?>';
 }
 
 function but_editcode(){
  document.getElementById('buf').value+="\r\n"+'<?php
   echo ('<a href="/i/editcode.php?file=<'.'?php echo urlencode($sad_script); ?'.'>" target="_blank" title="Edit code of this page"><img src="/i/style/blue-document-attribute-p.png" style="border: none;" alt=" Edit code of this page "></a>');
  ?>';
 }
</script>

<?php }
 if (preg_match('|^edit/|',$f1)){
  $special=true;
?>
 <span style="float: right; width: 165px;">
  <abbr title="Textbox">fieldname label</abbr><br /><br />
  <abbr title="Textarea">fieldname [simpletext] label</abbr><br /><br />
  <abbr title="TinyMCE">fieldname [text] label</abbr><br /><br />
  <abbr title="Calendar">fieldname [date] label</abbr><br /><br />
  <abbr title="Checkbox">fieldname [checkbox] label</abbr><br /><br />
  <abbr title="Image">fieldname [image] {<span style="color: gray">max_width max_height max_size_in_bytes</span> "folder/for/file" <span style="color: gray">nomd5</span> [<span style="color: gray">"folder/for/thumb" "prefix_for_file" "filetype_png/jpg/gif"</span> width_or_auto height_or_auto]} label</abbr><br /><br />
 </span>

<?php } ?>

 <textarea style="width: <?php if ($special){echo '700px';}else{echo '98%';} ?>; height: 500px; " id="buf" name="buf"><?php echo sad_safe_html(file_get_contents($file)); ?></textarea>
 <input type="hidden" name="file" value="<?php echo sad_safe_html($f1);?>"><br>
 <input type="submit" class="nicebutton mrgn" value="Write File">
</form>


</body></html>